package com.example.inperson;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

public class Loading extends Activity {
	private Handler mHandler = new Handler();
	private ProgressBar mProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		runLoad.run();
	}
	
	// This loading bar would usually be updated as the app searches for matches.
	Runnable runLoad = new Runnable() {		
		@Override
		public void run() {
			if ((mProgress = (ProgressBar) findViewById(R.id.progressBar1)) == null) {
				Log.d("lol", "Waiting 100ms for progressBar1 to be not-null.");
				mHandler.postDelayed(runLoad, 100);
			}
			else if (mProgress.getProgress() < 100) {
				if (Math.random() < 0.5)
					mProgress.setProgress((int) (mProgress.getProgress() + Math.random() * 10));
				mHandler.postDelayed(runLoad, (int) (Math.random() * 100));
			} else {
				proceedToNextActivity();
			}
		}
	};
	
	public void proceedToNextActivity() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Match found! Would you like to meet up with them?")
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	Intent myIntent = new Intent(getBaseContext(), CompassActivity.class);
        		// myIntent.putExtra("key", value); //Optional parameters
        		Loading.this.startActivity(myIntent);
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            	mProgress.setProgress(0);
				mHandler.postDelayed(runLoad, (int) (Math.random() * 200));
            }
        }).setCancelable(false);
		builder.create().show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.loading, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_loading,
					container, false);
			return rootView;
		}
	}

}
